package com.yishe.controller.wx.common;

import com.yishe.biz.wx.common.WxCommonBiz;
import com.yishe.config.ObjectRestResponse;
import com.yishe.constant.WxConstants;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.util.Map;

/**
 * @author : chusifeng
 * @version :
 * @date :Create in 2021/4/14 14:35
 * @description: 微信用户系统工具action
 * @package com.yishe.biz.wx.common
 * @className WxCommonController
 */
@RestController
@Slf4j
@RequestMapping("/wx/common/")
public class WxCommonController {
    @Resource
    private WxCommonBiz wxCommonBiz;

    /**
     * 判断手机号是否注册
     */
    @RequestMapping(value = "checkPhone", method = RequestMethod.POST)
    public ObjectRestResponse checkPhone(@RequestBody Map<String, String> param) {
        ObjectRestResponse response = new ObjectRestResponse();
        try {
            String result = wxCommonBiz.checkPhone(param);
            if (WxConstants.EMPTY.equals(result)) {
                response.setSuccessMsg("ok");
            } else {
                response.setErrorMsg(result);
            }
        } catch (Exception e) {
            log.error("根据手机号查询用户注册信息出错：", e);
            e.printStackTrace();
            response.setErrorMsg(e.getMessage());
        }
        return response;
    }

    /**
     * 微信用户注册
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public ObjectRestResponse register(@RequestBody Map<String, String> param) {
        ObjectRestResponse response = new ObjectRestResponse();
        try {
            String result = wxCommonBiz.register(param);
            if (StringUtils.isBlank(result)) {
                response.setSuccessMsg("注册成功");
            } else {
                response.setErrorMsg(result);
            }
        } catch (Exception e) {
            log.error("用户系统注册成功出错：", e);
            e.printStackTrace();
            response.setErrorMsg(e.getMessage());
        }
        return response;
    }

    /**
     * 从微信服务器下载上传的图片影像缓存到本机
     */
    @RequestMapping("/downloadImgFronWx")
    public ObjectRestResponse downloadImgFronWx(@RequestBody Map<String, String> medicalMap) throws WxErrorException {
        ObjectRestResponse restResponse = new ObjectRestResponse();
        String medical_id = medicalMap.get("medical_id");
        if (StringUtils.isBlank(medical_id)) {
            restResponse.setErrorMsg("图片参数有误！");
            return restResponse;
        }
        try {
            String flag = wxCommonBiz.downloadImgFronWx(medical_id);
            if (!"error".equals(flag)) {
                restResponse.setSuccessMsg(flag);
            } else {
                restResponse.setErrorMsg("上传失败！");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.error("图片上传失败！", e);
            restResponse.setErrorMsg("上传失败！");
        }
        return restResponse;
    }
}
